package entities
{
	import org.flixel.*;
	import util.DynamicText;

	public class HUD extends FlxGroup
	{
		public var lvlText:DynamicText;
		public var resetText:DynamicText;
		private var TimeTxt:FlxText;
		private var gameTimer:FlxTimer;	
		
		public function HUD() 
		{
			super();
				
			gameTimer  = new FlxTimer();
			

			TimeTxt = new FlxText(FlxG.width -50, 10, 50);
			TimeTxt.size = 18;
			TimeTxt.scrollFactor.x = 0;
			TimeTxt.scrollFactor.y = 0;
			TimeTxt.shadow = 0xffffffff;
			TimeTxt.color = 0xff333333;
			add(TimeTxt);
			
		}
				
		public function startTimer(time:int):void
		{
			gameTimer.start(time,1,onTimer);
		}
		
		override public function update():void
		{
			super.update();
			
			if(gameTimer.timeLeft > 10){
				TimeTxt.text = Math.floor(gameTimer.timeLeft).toString();
				}
			else{
				TimeTxt.text = Math.floor(gameTimer.timeLeft).toString();
				TimeTxt.color = 0xffff0000;
				TimeTxt.size = 20;
				if (gameTimer.timeLeft == 0) TimeTxt.text = "0";
			}
			
			if (FlxG.keys.P){ 
				gameTimer.paused = !gameTimer.paused;
			}
		}

		private function onTimer(Timer:FlxTimer):void
		{
			FlxG.shake(0.01, 0.2, onShake);
		}
		
		protected function onShake():void
		{
			FlxG.fade(0xff000000, 2, onFade);
		}
		
		protected function onFade():void
		{		
			FlxG.resetState();
		}
		
		public function showLevel():void
		{
			lvlText = new DynamicText(FlxG.width / 3, FlxG.height / 4, 300, "Nivel " + Registry.currentLevel.toString(),40).fadeAndDie(200);
			//resetText = new DynamicText(50, 10, 300, "R to reset",20).fadeAndDie(1000); TODO: hay qu arreglar el tema de los relojes antes de habilitarlo
		}
		
	}

}